package de.homelabs.desktop.qtsandbox;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import com.trolltech.qt.gui.QApplication;
import com.trolltech.qt.gui.QMainWindow;
import com.trolltech.qt.gui.QMenu;

/**
 * implements a basic qt application
 * @author d.mueller
 *
 */
public class BaseApplication extends QMainWindow{

	private static final String APPLICATION_TITLE 	= "Base Application";
	private static final SimpleDateFormat SD 		= new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
	private static final int MIN_WIDTH 				= 300;
	private static final int MIN_HEIGHT				= 300;
	/**
	 * constructor
	 */
	public BaseApplication(){
		initUI();
	}
	

	/**
	 * initalize user interface
	 */
	private void initUI(){
		//set size to min
		this.setMinimumHeight(MIN_HEIGHT);
		this.setMinimumWidth(MIN_WIDTH);
		
		//default menu
		initMenu();
		
		//statusbar
		this.statusBar().setToolTip("This is the Statusbar");
		messageToStatusBar("Application startet", true);
	}
	
	/**
	 * init menu
	 */
	private void initMenu(){
		//add a file menu
		QMenu fileMenu = this.menuBar().addMenu("Datei");
		//add a quitt entry with quit action to the menu
		fileMenu.addAction("beenden", this, "quittAction()");
	}
	
	/**
	 * action handler for quitt
	 */
	private void quittAction(){
		//call application quitt
		QApplication.quit();
	}
	
	/**
	 * write a message to the statusbar with/without timestamp
	 * 
	 * @param message - messag to write
	 * @param withTimeStamp - flag for timestamp
	 */
	private void messageToStatusBar(String message, boolean withTimeStamp){
		if (withTimeStamp){
			this.statusBar().showMessage("["+SD.format(Calendar.getInstance().getTime())+"] "+message);
		} else {
			this.statusBar().showMessage(message);
		}
	}
	
	/**
	 * main 
	 * @param args
	 */
	public static void main (String[] args){
		QApplication.initialize(args);
		BaseApplication app = new BaseApplication();
		app.setWindowTitle(APPLICATION_TITLE);
		app.show();
		QApplication.exec();
	}
}
